#ifndef __FF_RTMP_H__
#define __FF_RTMP_H__

#ifdef __cplusplus
extern "C" {
#endif

#include <stdio.h>

#include <libavformat/avformat.h>
#include <libavutil/mathematics.h>
#include <libavutil/opt.h>
#include <libavutil/dict.h>
#include <libavutil/time.h>

#define MAX_RTMP_URL_SIZE 511
typedef int (*ffrtmp_read_func)(void *ctx,char *frame,int size,int64_t *pts,int *keyflag);
typedef struct
{
    AVFormatContext *ifmt_ctx[2];// video, or video+audio
    AVFormatContext *ofmt_ctx;

    AVBSFContext *bsf_ctx ;

    //raw stream file handle when used
    void *ctx_h264;
    void *ctx_aac;
    void *ctx_g711;
    ffrtmp_read_func h264_func;
    ffrtmp_read_func g711_func;
    ffrtmp_read_func aacs_func;

    int infile_num;
    int video_index;
    int audio_index;
    int video_frames_num;
    int audio_frames_num;
    char outurl[MAX_RTMP_URL_SIZE+1];

    int64_t start_time;
}ff_rtmp_ctx;


int ff_init_rtmp(ff_rtmp_ctx *myctx,const char *out);
int ff_exit_rtmp(ff_rtmp_ctx *myctx);

int ff_add_streams_from_file(ff_rtmp_ctx *myctx, const char *infile);
int ff_start_rtmp(ff_rtmp_ctx *myctx);
int ff_stop_rtmp(ff_rtmp_ctx *myctx);

int ff_add_264_stream(ff_rtmp_ctx *myctx,int w,int h,int fps,int brate, uint8_t *extra,int extrasize,void *h264ctx,ffrtmp_read_func h264cb);
int ff_add_aac_stream(ff_rtmp_ctx *myctx,int chns,int samplerate,int bits, void *aactx,ffrtmp_read_func aacb);
int ff_add_pcma_stream(ff_rtmp_ctx *myctx,int chns,int samplerate,int bits,void *g711ctx,ffrtmp_read_func g711cb);
/*
 *
 *
 * @param[in] myctx ff_rtmp_ctx context
 * @param[out] got_size ffmpeg read packet size from streams
 * @ret 0 - read stream data empty or write frame data success
 *	   -1 - get av_packet fail by @ctx_g711 or @ctx_h264
 *	   -2 - get audio frame fail by @ifmt_ctx[1]
 *	   -3 - get video frame fail by @ifmt_ctx[0]
 *	   -4 - no valid input streams are found
 *	   -5 - av_interleaved_write_frame api fail
 *     -32- Broken Pipe, server close connection
 *    -104- Connection reset by peer
 *    -110- Connection timed out
 */
int ff_write_rtmp_frame(ff_rtmp_ctx *myctx,int *got_size);

#ifdef __cplusplus
}
#endif

#endif

